package Draw;

import java.util.Vector;
public class KlmTank extends Tank implements Runnable{
    Vector<fright> frights=new Vector<>();
    Vector<KlmTank> klmTanks=new Vector<>();
    boolean loop=true;
    public KlmTank(int x, int y) {
        super(x, y);
    }

    public void setKlmTanks(Vector<KlmTank> klmTanks) {
        this.klmTanks = klmTanks;
    }
    public boolean istrue(){
    //坦克不重叠
            switch (this.getDirection()) {
                case 0:
                    for (int i = 0; i < klmTanks.size(); i++) {
                        if (klmTanks.get(i) != this) {
                            if (klmTanks.get(i).getDirection() == 0 || klmTanks.get(i).getDirection() == 2) {
                                if (this.getY() <= klmTanks.get(i).getY() + 40 &&
                                        this.getY() + 60 >= klmTanks.get(i).getY() &&
                                        this.getX() <= klmTanks.get(i).getX() + 60 &&
                                        this.getX() + 40 >= klmTanks.get(i).getX()) {
                                    return true;
                                }
                                if (this.getY() <= klmTanks.get(i).getY() + 60 &&
                                        this.getY()  >= klmTanks.get(i).getY() &&
                                        this.getX() <= klmTanks.get(i).getX() + 40 &&
                                        this.getX() >= klmTanks.get(i).getX()){
                                    return true;
                                }
                            }
                                if (klmTanks.get(i).getDirection() == 1 || klmTanks.get(i).getDirection() == 3) {
                                    if (this.getY() <= klmTanks.get(i).getY() + 60 &&
                                            this.getY() >= klmTanks.get(i).getY() &&
                                            this.getX() + 40 <= klmTanks.get(i).getX() + 40 &&
                                            this.getX() + 40 >= klmTanks.get(i).getX()) {
                                        return true;
                                    }
                                    if (this.getY() <= klmTanks.get(i).getY() + 40 &&
                                            this.getY() >= klmTanks.get(i).getY() &&
                                            this.getX()  <= klmTanks.get(i).getX() + 60 &&
                                            this.getX()  >= klmTanks.get(i).getX()){
                                        return true;
                                    }
                                }
                            }
                        }
                    break;
                case 1:
                    for (int i = 0; i < klmTanks.size(); i++) {
                        if (klmTanks.get(i) != this) {
                            if (klmTanks.get(i).getDirection() == 0 || klmTanks.get(i).getDirection() == 2) {
                                if (this.getY() <= klmTanks.get(i).getY() + 60 &&
                                        this.getY()  >= klmTanks.get(i).getY() &&
                                        this.getX() +60<= klmTanks.get(i).getX() + 40 &&
                                        this.getX() + 60 >= klmTanks.get(i).getX()) {
                                    return true;
                                }
                                if (this.getY()+40 <= klmTanks.get(i).getY() + 40 &&
                                        this.getY() +40 >= klmTanks.get(i).getY() &&
                                        this.getX() +60<= klmTanks.get(i).getX() + 40 &&
                                        this.getX() + 60 >= klmTanks.get(i).getX()){
                                    return true;
                                }
                            }
                            if (klmTanks.get(i).getDirection() == 1 || klmTanks.get(i).getDirection() == 3) {
                                if (this.getY()+40<= klmTanks.get(i).getY() + 60 &&
                                        this.getY() +40>= klmTanks.get(i).getY() &&
                                        this.getX() + 60 <= klmTanks.get(i).getX() + 40 &&
                                        this.getX() + 60 >= klmTanks.get(i).getX()) {
                                    return true;
                                }
                                if (this.getY() <= klmTanks.get(i).getY() + 40 &&
                                        this.getY() >= klmTanks.get(i).getY() &&
                                        this.getX() +60 <= klmTanks.get(i).getX() + 60 &&
                                        this.getX() +60 >= klmTanks.get(i).getX()){
                                    return true;
                                }
                            }
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < klmTanks.size(); i++) {
                        if (klmTanks.get(i) != this) {
                            if (klmTanks.get(i).getDirection() == 0 || klmTanks.get(i).getDirection() == 2) {
                                if (this.getY()+60 <= klmTanks.get(i).getY() + 60 &&
                                        this.getY()+60  >= klmTanks.get(i).getY() &&
                                        this.getX() <= klmTanks.get(i).getX() + 40 &&
                                        this.getX()  >= klmTanks.get(i).getX()) {
                                    return true;
                                }
                                if (this.getY()+40 <= klmTanks.get(i).getY() + 60 &&
                                        this.getY() +40 >= klmTanks.get(i).getY() &&
                                        this.getX() +60<= klmTanks.get(i).getX() + 40 &&
                                        this.getX() + 60 >= klmTanks.get(i).getX()){
                                    return true;
                                }
                            }
                            if (klmTanks.get(i).getDirection() == 1 || klmTanks.get(i).getDirection() == 3) {
                                if (this.getY()+60<= klmTanks.get(i).getY() + 40 &&
                                        this.getY() +60>= klmTanks.get(i).getY() &&
                                        this.getX() + 40 <= klmTanks.get(i).getX() + 60 &&
                                        this.getX() + 40 >= klmTanks.get(i).getX()) {
                                    return true;
                                }
                                if (this.getY() <= klmTanks.get(i).getY() + 40 &&
                                        this.getY() >= klmTanks.get(i).getY() &&
                                        this.getX()  <= klmTanks.get(i).getX() + 60 &&
                                        this.getX()  >= klmTanks.get(i).getX()){
                                    return true;
                                }
                            }
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < klmTanks.size(); i++) {
                        if (klmTanks.get(i) != this) {
                            if (klmTanks.get(i).getDirection() == 0 || klmTanks.get(i).getDirection() == 2) {
                                if (this.getY()<= klmTanks.get(i).getY() + 60 &&
                                        this.getY()  >= klmTanks.get(i).getY() &&
                                        this.getX() <= klmTanks.get(i).getX() + 40 &&
                                        this.getX()  >= klmTanks.get(i).getX()) {
                                    return true;
                                }
                                if (this.getY()+40 <= klmTanks.get(i).getY() + 60 &&
                                        this.getY() +40 >= klmTanks.get(i).getY() &&
                                        this.getX() <= klmTanks.get(i).getX() + 40 &&
                                        this.getX() >= klmTanks.get(i).getX()){
                                    return true;
                                }
                            }
                            if (klmTanks.get(i).getDirection() == 1 || klmTanks.get(i).getDirection() == 3) {
                                if (this.getY()+40<= klmTanks.get(i).getY() + 40 &&
                                        this.getY() +40>= klmTanks.get(i).getY() &&
                                        this.getX() <= klmTanks.get(i).getX() + 60 &&
                                        this.getX() >= klmTanks.get(i).getX()) {
                                    return true;
                                }
                                if (this.getY() <= klmTanks.get(i).getY() + 40 &&
                                        this.getY() >= klmTanks.get(i).getY() &&
                                        this.getX()  <= klmTanks.get(i).getX() + 60 &&
                                        this.getX()  >= klmTanks.get(i).getX()){
                                    return true;
                                }
                            }
                        }
                    }
                    }
             return false;
            }
    @Override
    public void run() {
        while (true){
            if(loop&&frights.size()<5){
               fright fright3=null;
               switch (getDirection()){
                   case 0:
                       fright3=new fright(getX()+20,getY(),getDirection());
                       break;
                   case 1:
                       fright3=new fright(getX()+60,getY()+20,getDirection());
                       break;
                   case 2:
                       fright3=new fright(getX()+20,getY()+60,getDirection());
                       break;
                   case 3:
                       fright3=new fright(getX(),getY()+20,getDirection());
                       break;
               }
               frights.add(fright3);
               new Thread(fright3).start();
            }
            switch (getDirection()){
                case 0:
                    for (int i = 0; i < getSpeed(); i++) {
                        if (getY()>0&&!istrue()){
                        Up();}
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        }break;
                case 1:
                    for (int i = 0; i < getSpeed(); i++) {
                        if (getX()+60<900&&!istrue()){
                        Left();}
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        }break;
                case 2:  for (int i = 0; i < getSpeed(); i++) {
                    if (getY()+60<900&&!istrue()){
                        Down();}
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    }break;
                case 3:  for (int i = 0; i < getSpeed(); i++) {
                    if(getX()>0&&!istrue())
                    Right();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }break;
            }
            setDirection((int)(Math.random()*4));
            if (!loop){//坦克被击中，退出线程
                break;
            }
        }
    }
}
